package com.nuo.mybatis;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.hibernate.Session;

public class SessionFactory {
	
	private static SqlSessionFactory sqlSessionFactory = null;
	
	private static void init() {
		String resource = "mybatis-config.xml";
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			System.out.println("无法找到配置文件!");
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

	}
	
	public static SqlSession getSession() {
		if(sqlSessionFactory == null){
			init();
		}
		if(sqlSessionFactory != null){
			return sqlSessionFactory.openSession();
		}else{
			return null;
		}
	}
	
}
